//
//  ZLoginVC.m
//  WeClubs
//
//  Created by Zed Link on 28/3/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZLoginVC.h"
#import "ZTabbarController.h"
#import "ZPhoneVerifyVC.h"
#import "ZPasswordVC.h"
#import "ZLoginUserModel.h"
#import "ZLoginHandle.h"
#import "ZForgetPasswordVC.h"


@interface ZLoginVC () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;

@end

@implementation ZLoginVC

+ (instancetype)zNew
{
    return [self zStoryboardInstanceWithStoryboardName:@"LoginAndRegister"];
}

#pragma mark - About Navigation

- (void)zConfigNavigation
{
    [super zConfigNavigation];
    
    [self zClearNavBgColorAndSeparator:NO];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:LOGIN_VIEW_BG_COLOR];
    
    // 字数限制
    [self.phoneField addTarget:self action:@selector(phoneNumberDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.phoneField.text = @"13077847205";
    self.pwdField.text = @"123456";
}

- (void)phoneNumberDidChange:(UITextField *)textField
{
    if (textField == self.phoneField && textField.text.length > 11)
    {
        textField.text = [textField.text substringToIndex:11];
        // 提示 电话号码最多11位
        [MBProgressHUD zAlertError:@"电话号码最多11位"];
    }
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == _pwdField) {
        [self loginAction:nil];
    }
    return YES;
}

#pragma mark - Control Action

- (IBAction)blankAction:(id)sender
{
    // 点击空白, 回收键盘
    [self.view endEditing:YES];
}

- (IBAction)registerAction:(id)sender
{
    // 跳转到 <用户注册>
    ZPhoneVerifyVC *verifyVC = [ZPhoneVerifyVC zNew];
    [self.navigationController pushViewController:verifyVC animated:YES];
}

- (IBAction)loginAction:(id)sender
{
    NSString *phoneNumber = self.phoneField.text;
    NSString *pwd = self.pwdField.text;
    
    if (pwd.length == 0) {
        
        [MBProgressHUD zAlertError:@"请输入密码"];
        return;
    }
    // Tips: 手机号码错误提示
    if ([phoneNumber zCheckPhoneNumber] == NO) {
        [MBProgressHUD zAlertToastMessage:kPhoneError];
        return;
    }

    NSDictionary *dataPara = @{
                               @"mobile" : phoneNumber,
                               @"password" : [pwd zMD5HexString],
                               @"notoken": @"notoken",
                               };
    
    // 正在登陆
    [MBProgressHUD zAlertProgress:@"正在登陆...."];
    [HTTPRequest POST:kLoginURL parameter:dataPara success:^(id responseObject) {
        
        [MBProgressHUD zAlertHideHUD];
        ZLoginUserModel *userModel = [[ZLoginUserModel alloc] initWithDictionary:responseObject[@"data"]];
        if ([responseObject[@"result_code"] intValue]== ZNetManagerStatusSuccess) {
            
        // 暂时不保存uuid和token以后记得打开
            [ZUserInfo sharedInstance].userId = userModel.userId;
            [ZUserInfo sharedInstance].token = userModel.token;
            
            [self thirdConfig];
            ZTabbarController *tabVC = [[ZTabbarController alloc] init];
            [self.view.window setRootViewController:tabVC];
        }else{
            [MBProgressHUD zAlertToastMessage:responseObject[@"result_msg"]];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD zAlertHideHUD];
        
        NSString *errorMsg = error.userInfo[@"message"];
        
        [MBProgressHUD zAlertError:errorMsg];
    }];
  
}

- (IBAction)forgetPwdAction:(id)sender
{
    // Todo: 找回密码
    ZForgetPasswordVC *verifyVC = [ZForgetPasswordVC zNew];
    verifyVC.view.backgroundColor = [UIColor whiteColor];
    [self.navigationController pushViewController:verifyVC animated:YES];
}

// config
- (void)thirdConfig{
    [[ZLoginHandle shareInstance] JPushConfig];
    [[ZLoginHandle shareInstance] RongCloudConfig];
}

@end
